TWB.New_Module({
	///////////////////////////
	name : "Order",	 	     //
	author : "enduo",        //
	version : 1.0,           //
	///////////////////////////

	Distance : function(){
		if(!TWB.logged()){
			return alert(TWB.lang("notlogged"));
		}
		
		var i;
		var ordered=[];
		var V=TWB.gei("TWB-VillageList");
		var S=TWB.gei("TWB-SandboxList");
		if(TWB.Modules.Icons.Screen()=="info_village"){
			var target=TWB.Xpath("//*[@id='content_value']//tr/td/table[@class='vis']//tr[2]/td[2]",9).innerHTML.split("|");
		}
		else{
			var WN=TWB._window();
			if(typeof WN.wrappedJSObject!="undefined"){
				WN=WN.wrappedJSObject;
			}
			var target=WN.game_data.village.coord.split("|")
		}
		for(i=0; i<V.childNodes.length; i++){
			var xy=V.childNodes[i].getAttribute("tooltiptext").match(/\d+\|\d+/)[0].split("|");
			var letc=Math.sqrt(Math.pow(xy[0]-target[0],2)+Math.pow(xy[1]-target[1],2));
			ordered.push([letc/10000,V.childNodes[i].getAttribute("id")]);
		}
		// Move to sandbox
		for(i=V.childNodes.length-1; i>0; i--){
			S.appendChild(V.childNodes[i]);
		}
		// Clear Village List
		TWB.Modules.Villages.ClearAll();
		ordered.sort();
		for(i=0; i<ordered.length; i++){
			// populate
			V.appendChild(TWB.gei(ordered[i][1]));
		}
		TWB.Modules.Villages.ClearSandBox();
	},
	Name : function(){
		if(!TWB.logged()){
			return alert(TWB.lang("notlogged"));
		}
		
		var i;
		var ordered=[];
		var V=TWB.gei("TWB-VillageList");
		var S=TWB.gei("TWB-SandboxList");
		for(i=0; i<V.childNodes.length; i++){
			ordered.push([V.childNodes[i].getAttribute("label"),V.childNodes[i].getAttribute("id")]);
		}
		// Move to sandbox
		for(i=V.childNodes.length-1; i>0; i--){
			S.appendChild(V.childNodes[i]);
		}
		ordered.sort();
		// Clear Village List
		TWB.Modules.Villages.ClearAll();
		for(i=0; i<ordered.length; i++){
			// populate
			V.appendChild(TWB.gei(ordered[i][1]));
		}
		TWB.Modules.Villages.ClearSandBox();
	}
});